/*
 * @# AlbumServiceImpl.java 2011-12-23 下午04:23:29
 * 
 * Copyright (C) 2010 - 2011 广州羿安信息科技有限公司
 * Guangzhou ianswer information technology co. ltd.
 * 
 * All rights reserved!
 */
package com.ianswer.px.album.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ianswer.common.orm.PublicDao;
import com.ianswer.common.util.time.SecondsUtils;
import com.ianswer.px.album.entity.Album;
import com.ianswer.px.album.redis.AlbumRedis;
import com.ianswer.px.album.service.AlbumService;

/**
 * 相册处理逻辑.
 * 
 * @author alvin hwang
 */
@Service("albumService")
public class AlbumServiceImpl implements AlbumService {

	@Resource(name = "publicDao")
	private PublicDao publicDao;
	
	@Override
	public List<Album> queryLastestShowAlbums(Long userId) {
		Album album = new Album();
		album.setUserId(userId);
		
		List<Album> albums = publicDao.query("albums.queryLastestShowAlbums", album);
		return completeNums(albums);
	}
	
	/**
	 * 补充完整相关的数字(评论数等).
	 */
	private List<Album> completeNums(List<Album> albums) {
		if (CollectionUtils.isEmpty(albums)) {
			return albums;
		}
		for (Album album : albums) {
			completeNums(album);
		}
		return albums;
	}
	
	private Album completeNums(Album album) {
		if (null == album) {
			return new Album();
		}
		Album albumCache = AlbumRedis.get(album.getId());
		
		if (albumCache == null) {
			return album;
		}
		
		album.setCreatedAt(albumCache.getCreatedAt());
		album.setComments(albumCache.getComments());
		album.setCommentsTotal(albumCache.getCommentsTotal());
		album.setFavors(albumCache.getFavors());
		album.setLikes(albumCache.getLikes());
		album.setPhotos(albumCache.getPhotos());
		album.setTransferred(albumCache.getTransferred());
		
		return album;
	}

	@Override
	public List<Album> queryShowAlbums(Long userId) {
		Album album = new Album();
		album.setUserId(userId);
		
		List<Album> albums = publicDao.query("albums.queryShowAlbums", album);
		return albums;
	}

	@Override
	public List<Album> queryShowAlbums(Long userId, Long galleryId) {
		Album album = new Album();
		album.setUserId(userId);
		album.setGalleryId(galleryId);
		
		List<Album> albums = publicDao.query("albums.queryShowAlbums", album);
		return completeNums(albums);
	}

	@Override
	public Album get(Long albumId) {
		Album album = publicDao.querySingle("albums.get", albumId);
		return completeNums(album);
	}

	@Override
	public Long insert(Album album) {
		album.setCreatedAt(SecondsUtils.seconds());
		Long albumId = (Long) publicDao.insert("albums.insert", album);
		
		AlbumRedis.init(album);
		
		return albumId;
	}

	@Override
	public void update(Album album) {
		publicDao.update("albums.update", album);
	}

	@Transactional
	@Override
	public void delete(Long userId, Long albumId) {
		Album album = new Album();
		album.setUserId(userId);
		album.setId(albumId);
		publicDao.delete("albums.delete", album);
		publicDao.delete("albums.deletePhotos", album);
		
		AlbumRedis.delete(albumId);
	}

}
